import java.io.BufferedReader;
import java.io.InputStreamReader;

public class USBBootMaker {

    public static void main(String[] args) {
        if (args.length != 2) {
            System.out.println("Usage: java USBBootMaker <iso-file> <device>");
            System.exit(1);
        }

        String isoFile = args[0];
        String device = args[1];

        // 检查设备是否有效
        if (!isValidDevice(device)) {
            System.out.println("Invalid device specified.");
            System.exit(1);
        }

        // 使用dd命令写入ISO到USB设备
        try {
            Process process = Runtime.getRuntime().exec("sudo dd if=" + isoFile + " of=" + device + " bs=4M status=progress");
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
            process.waitFor();
            System.out.println("Operation completed successfully.");
        } catch (Exception e) {
            System.err.println("An error occurred: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static boolean isValidDevice(String device) {
        // 这里可以添加更复杂的验证逻辑
        return device.startsWith("/dev/sd") || device.startsWith("/dev/rdisk");
    }
}